package com.kfy.observerdemo.Pub_Sub;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: ObserverDemo
 * @Package: com.kfy.observerdemo.Pub_Sub
 * @Author: K.F.Y
 * @Date: 2021/5/8
 */
public class SubscribePublish {

    private String name;

    private List<ISubcriber> subcribers = new ArrayList<>();

    public SubscribePublish(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    //添加订阅者
    public void attach(ISubcriber subcriber){
        subcribers.add(subcriber);
    }

    //删除订阅者
    public void detach(ISubcriber subcriber){
        subcribers.remove(subcriber);
    }

    //发布者发布消息，接收者接收信息
    public void publishAndUpdate(IPublisher publisher){
        for (ISubcriber subcriber : subcribers){
            subcriber.update(publisher);
        }
    }

}
